package com.smag.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.smag.common.constant.SystemConstants;
import com.smag.common.core.domain.entity.FarmProduct;

import com.smag.common.core.page.TableDataInfo;
import com.smag.common.utils.StringUtils;
import com.smag.common.utils.bean.BeanUtils;
import com.smag.system.domain.dto.ProductQueryDto;
import com.smag.system.domain.vo.ProductVo;
import com.smag.system.mapper.FarmProductMapper;
import com.smag.system.service.FarmCategoryService;
import com.smag.system.service.FarmProductService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author hzqhzq
 * @description 针对表【farm_product】的数据库操作Service实现
 * @createDate 2023-03-31 10:19:04
 */
@Service
public class FarmProductServiceImpl extends ServiceImpl<FarmProductMapper, FarmProduct>
        implements FarmProductService {
    @Resource
    private FarmProductMapper farmProductMapper;

    @Resource
    private FarmCategoryService farmCategoryService;

    @Override
    public List<FarmProduct> selectFarmProductList(ProductQueryDto farmProduct, Long userId) {
        LambdaQueryWrapper<FarmProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), FarmProduct::getCreateBy, userId);
        queryWrapper.like(StringUtils.isNotEmpty(farmProduct.getName()), FarmProduct::getName, farmProduct.getName());
        if (farmProduct.getStatus()) {
            queryWrapper.eq(FarmProduct::getStatus, SystemConstants.PRODUCT_STATUS_NORMAL);
        } else {
            queryWrapper.eq(FarmProduct::getStatus, SystemConstants.PRODUCT_STATUS_ABNORMAL);
        }
        queryWrapper.ge(FarmProduct::getPrice, farmProduct.getPrices().get(0));
        if (farmProduct.getPrices().get(1) != 2000) {
            queryWrapper.le(FarmProduct::getPrice, farmProduct.getPrices().get(1));
        }
        queryWrapper.eq(ObjectUtils.isNotEmpty(farmProduct.getAmount()), FarmProduct::getAmount, farmProduct.getAmount());
        if (farmProduct.getSort()) {
            queryWrapper.orderByDesc(FarmProduct::getSales);
        } else {
            queryWrapper.orderByAsc(FarmProduct::getSales);
        }
        queryWrapper.like(StringUtils.isNotEmpty(farmProduct.getAddress()), FarmProduct::getAddress, farmProduct.getAddress());
        return list(queryWrapper);
    }

    @Transactional
    @Override
    public boolean updateTechAvatar(Long productId, String avatar) {
        UpdateWrapper<FarmProduct> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("id", productId);
        queryWrapper.set("avatar", avatar);
        return update(queryWrapper);
    }

    @Override
    public boolean insertFarmProduct(FarmProduct farmProduct) {
        return save(farmProduct);
    }
    @Override
    public boolean updateFarmProduct(FarmProduct farmProduct) {
        return updateById(farmProduct);
    }

    @Override
    public FarmProduct selectFarmProductById(Long productId) {
        return getById(productId);
    }

    @Override
    public int deleteFarmProductByIds(Long[] ids) {
        return farmProductMapper.updateStatusBatchById(Arrays.asList(ids), SystemConstants.PRODUCT_STATUS_ABNORMAL);
    }

    @Override
    public int updateFarmProductStatus(Long[] ids) {
        return farmProductMapper.updateStatusBatchById(Arrays.asList(ids), SystemConstants.PRODUCT_STATUS_NORMAL);
    }

    @Override
    public List<?> selectRecFarmProduct() {
        LambdaQueryWrapper<FarmProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FarmProduct::getStatus,SystemConstants.PRODUCT_STATUS_NORMAL);
        queryWrapper.orderByDesc(FarmProduct::getSales);
        List<FarmProduct> list = list(queryWrapper);
        return BeanUtils.copyBeanList(list, ProductVo.class);
    }

    @Override
    public TableDataInfo listProducts(Integer pageNum,Integer pageSize,String productName, Long minPrice, Long maxPrice, Boolean sort,Long categoryId) {
        if (minPrice != null && maxPrice != null){
            if (minPrice > maxPrice){
                Long temp = minPrice;
                minPrice = maxPrice;
                maxPrice = temp;
            }
        }
        String sortName = null;
        if (sort != null){
            if (sort){
                sortName = "desc";
            }else {
                sortName = "asc";
            }
        }
        int count = farmProductMapper.selectListProductCount(productName,minPrice,maxPrice,sortName,categoryId);
        PageHelper.startPage(pageNum,pageSize);
        List<FarmProduct> list = farmProductMapper.selectListProduct(productName,minPrice,maxPrice,sortName,categoryId);
        return new TableDataInfo(BeanUtils.copyBeanList(list, ProductVo.class),count);
    }

    @Override
    public FarmProduct selectFarmProductByArticleId(Long id) {
        return farmProductMapper.selectFarmProductByArticleId(id);
    }

    @Override
    public List<FarmProduct> selectAllProduct() {
        return farmProductMapper.selectAllProduct();
    }

    @Override
    public Long selectProductIdByArticleId(Long id) {
        return farmProductMapper.selectProductIdByArticleId(id);
    }

}




